#!/bin/bash

plat=$1

ECHO="echo -e"

if [ -z "$plat" ]; then
    $ECHO "$0 CentOS-x64/x64/corei7"
    exit
fi

function log() {
    $ECHO "$*"
}

plat_less=${plat%-*}

## ready work directory
function ready_work_dir() {
    if [ "$SUDO" == "sudo" ]; then
        $SUDO chown $USER:$USER /opt
    fi
    ## put Qt directory
    if [ ! -d /opt/data/data/work ]; then
        mkdir -p /opt/data/data/work
    fi
}

function check_tools() {
    which apt &> /dev/null
    if [ $? -eq 0 ]; then
        INSTALL_TOOLS=apt
    else
        which apt-get &> /dev/null
        if [ $? -eq 0 ]; then
            INSTALL_TOOLS=apt-get
        else
            which yum &> /dev/null
            if [ $? -eq 0 ]; then
                INSTALL_TOOLS=yum
            fi
        fi
    fi
    USER=`whoami`
    if [ "$USER" != "root" ]; then
        SUDO=sudo
    fi
}

function install_tools() {
    tools=$*
    case $INSTALL_TOOLS in
    apt | apt-get | yum)
        $SUDO $INSTALL_TOOLS install $tools
    ;;
    esac
}

function install_common() {
    prog_name=$1
    which $prog_name &> /dev/null
    if [ $? -ne 0 ]; then
        install_tools $prog_name
    fi
}

function install_GL() {
    EGLName=mesa-libEGL-devel
    GLESName=mesa-libGLES-devel
    GLName=mesa-libGL-devel
    GLUName=mesa-libGLU-devel
    if [ "$plat_less" == "x64" ]; then
        EGLName=libegl1-mesa-dev
        GLESName=libgles2-mesa-dev
        GLName=libgl1-mesa-dev
        GLUName=libglu1-mesa-dev
    fi

    pkg-config --modversion egl
    if [ $? -ne 0 ]; then
        add_installs=" $EGLName"
    fi
    pkg-config --modversion gles
    if [ $? -ne 0 ]; then
        add_installs+=" $GLESName"
    fi
    pkg-config --modversion gl
    if [ $? -ne 0 ]; then
        add_installs+=" $GLName"
    fi
    pkg-config --modversion glu
    if [ $? -ne 0 ]; then
        add_installs+=" $GLUName"
    fi
    if [ -n "$add_installs" ]; then
        install_tools $add_installs
    fi
}

function install_xasm() {
    x_asm=yasm
    if [ "$plat_less" == "CentOS" ]; then
        x_asm=nasm
    fi
    install_common $x_asm
}

function install_patch() {
    install_common patch
}

function extern_tools() {

    which libtoolize &> /dev/null
    if [ $? -gt 0 ]; then
        log "Install libtool ..."
        install_tools libtool
    fi
    which autoconf  &> /dev/null
    if [ $? -gt 0 ]; then
        log "install autoconf ..."
        install_tools autoconf
    fi
    which git &> /dev/null
    if [ $? -gt 0 ]; then
        log "install git ..."
        install_tools git
    fi

    install_GL
    install_xasm
}

function install_compile_base() {
    which cmake &> /dev/null
    if [ $? -gt 0 ]; then
        log "Install cmake ..."
        install_tools cmake
    fi
    which gcc &> /dev/null
    if [ $? -gt 0 ]; then
        install_tools gcc
    fi
    which g++ &> /dev/null
    if [ $? -gt 0 ]; then
        if [ "$plat_less" == "x64" ]; then
            install_tools g++
        elif [ "$plat_less" == "CentOS" ]; then
            install_tools gcc-c++
        fi
    fi
}

function install_compile_lib_base() {
    ret=`pkg-config --modversion zlib`
    if [ $? -gt 0 ]; then
        if [ "$plat_less" == "x64" ]; then
            ZLIB_NAME="zlib1g-dev"
        elif [ "$plat_less" == "CentOS" ]; then
            ZLIB_NAME="zlib-devel"
        fi
        install_tools $ZLIB_NAME
    else
        log "Installed zlib version is $ret"
    fi
    ret=`pkg-config --modversion openssl`
    if [ $? -gt 0 ]; then
        if [ "$plat_less" == "x64" ]; then
            OPENSSL_NAME="libcurl4-openssl-dev"
        elif [ "$plat_less" == "CentOS" ]; then
            OPENSSL_NAME="openssl-devel"
        fi
        install_tools $OPENSSL_NAME
    else
        log "Installed openssl version is $ret"
    fi
}

check_tools
extern_tools
ready_work_dir

install_compile_base
install_compile_lib_base

